package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceWorkmanshipProcessRpcService;
import com.hunttown.mes.rpc.domain.MesProduceWorkmanshipProcessDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceWorkmanshipProcessDTOQuery;
import com.hunttown.mes.service.MesProduceWorkmanshipProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProduceWorkmanshipProcessConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:32:46
 */
@Service
public class MesProduceWorkmanshipProcessRpcServiceImpl implements MesProduceWorkmanshipProcessRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceWorkmanshipProcessRpcServiceImpl.class);

    private final MesProduceWorkmanshipProcessService mesProduceWorkmanshipProcessService;

    @Autowired
    public MesProduceWorkmanshipProcessRpcServiceImpl(MesProduceWorkmanshipProcessService mesProduceWorkmanshipProcessService) {
        this.mesProduceWorkmanshipProcessService = mesProduceWorkmanshipProcessService;
    }

    @Override
    public MesProduceWorkmanshipProcessDTO insert(MesProduceWorkmanshipProcessDTO mesProduceWorkmanshipProcessDTO) {
        try {
            return convert(mesProduceWorkmanshipProcessService.insert(convert(mesProduceWorkmanshipProcessDTO)));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess create failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceWorkmanshipProcessDTO mesProduceWorkmanshipProcessDTO) {
        try {
            return mesProduceWorkmanshipProcessService.updateInfoById(convert(mesProduceWorkmanshipProcessDTO));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess update by id failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceWorkmanshipProcessDTOQuery mesProduceWorkmanshipProcessDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceWorkmanshipProcessDTOQuery.getW_id() != null && mesProduceWorkmanshipProcessDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceWorkmanshipProcessDTOQuery.getW_id());
            }

            if (mesProduceWorkmanshipProcessDTOQuery.getIds() != null && !mesProduceWorkmanshipProcessDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceWorkmanshipProcessDTOQuery.getIds());
            }

            if (mesProduceWorkmanshipProcessDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceWorkmanshipProcessDTOQuery.getStopFlag());
            }

            if (mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag());
            }

            return mesProduceWorkmanshipProcessService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess update by query failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceWorkmanshipProcessDTO selectById(Integer id) {
        try {
            return convert(mesProduceWorkmanshipProcessService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess select by id failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceWorkmanshipProcessDTO selectObjByQuery(MesProduceWorkmanshipProcessDTOQuery mesProduceWorkmanshipProcessDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceWorkmanshipProcessDTOQuery.getShipId() != null) {
                query.put("shipId", mesProduceWorkmanshipProcessDTOQuery.getShipId());
            }

            if (mesProduceWorkmanshipProcessDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceWorkmanshipProcessDTOQuery.getStopFlag());
            }

            if (mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceWorkmanshipProcessDTOQuery.getOrderBy());
            }

            return convert(mesProduceWorkmanshipProcessService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess select obj by query failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceWorkmanshipProcessDTO> getListForPage(MesProduceWorkmanshipProcessDTOQuery mesProduceWorkmanshipProcessDTOQuery) {
        Query query = Query.page(mesProduceWorkmanshipProcessDTOQuery.getPn(), mesProduceWorkmanshipProcessDTOQuery.getPageSize());

        if (mesProduceWorkmanshipProcessDTOQuery.getId() != null && mesProduceWorkmanshipProcessDTOQuery.getId() > 0) {
            query.put("id", mesProduceWorkmanshipProcessDTOQuery.getId());
        }

        if (mesProduceWorkmanshipProcessDTOQuery.getIds() != null && !mesProduceWorkmanshipProcessDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceWorkmanshipProcessDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceWorkmanshipProcessDTOQuery.getLikeName());
        }

        if (mesProduceWorkmanshipProcessDTOQuery.getShipId() != null) {
            query.put("shipId", mesProduceWorkmanshipProcessDTOQuery.getShipId());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceWorkmanshipProcessDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceWorkmanshipProcessDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceWorkmanshipProcessDTOQuery.getEndDate());
        }

        if (mesProduceWorkmanshipProcessDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceWorkmanshipProcessDTOQuery.getStopFlag());
        }

        if (mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceWorkmanshipProcessDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipProcessDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceWorkmanshipProcessDTOQuery.getOrderBy());
        }

        List<MesProduceWorkmanshipProcessDTO> list = convert(mesProduceWorkmanshipProcessService.selectByQuery(query));
        Page<MesProduceWorkmanshipProcessDTO> page = Page.from(mesProduceWorkmanshipProcessDTOQuery.getPn(), mesProduceWorkmanshipProcessDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceWorkmanshipProcessService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceWorkmanshipProcessService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceWorkmanshipProcess delete failed.", e);
            throw new RuntimeException("MesProduceWorkmanshipProcess delete failed, Please order your code.", e);
        }
    }
}